package com.sywb.chuangyebao.view;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.contract.QAndADetailContract;
import com.sywb.chuangyebao.utils.Utils;

import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;
import org.bining.footstone.widget.PullToRefreshView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 问答详情
 */
public class QAndADetailActivity extends ActionbarActivity<QAndADetailContract.Presenter> implements QAndADetailContract.QADetailView {

    @BindView(R.id.common_recycler)
    RecyclerView commonRecycler;
    @BindView(R.id.common_refresh)
    PullToRefreshView commonRefresh;
    @BindView(R.id.common_nodata_content)
    TextView commonNodataContent;
    @BindView(R.id.common_nodata_icon)
    ImageView commonNodataIcon;
    @BindView(R.id.common_nodata_subtitle)
    TextView commonNodataSubtitle;
    @BindView(R.id.common_nodata)
    RelativeLayout commonNodata;
    @BindView(R.id.tv_consult)
    TextView tvConsult;
    @BindView(R.id.tv_collect)
    TextView tvCollect;
    @BindView(R.id.tv_invite)
    TextView tvInvite;
    @BindView(R.id.tv_answer)
    TextView tvAnswer;
    @BindView(R.id.ll_consult)
    LinearLayout llConsult;
    @BindView(R.id.trans_bg)
    View transBg;

    private int question_id;

    @Override
    public int getLayoutId(Bundle savedInstanceState) {
        return R.layout.activity_qanda_detail;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        if (savedInstanceState == null) {
            question_id = getIntent().getIntExtra("p0", 0);
        } else {
            question_id = savedInstanceState.getInt("p0");
        }

//        scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
//        scrollView.setFocusable(true);
//        scrollView.setFocusableInTouchMode(true);
//        scrollView.setOnTouchListener(new QADetailView.OnTouchListener() {
//            @Override
//            public boolean onTouch(QADetailView v, MotionEvent event) {
//                v.requestFocusFromTouch();
//                return false;
//            }
//        });

        setTitle(R.string.qanda_detail);
        addMenuImageItme(R.drawable.share_b, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.showDialog();
            }
        });
        commonRefresh.setBackgroundColor(ContextCompat.getColor(mActivity, R.color.colorLight));
        transBg.setVisibility(View.GONE);
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("p0", question_id);
        super.onSaveInstanceState(outState);
    }

    @OnClick({R.id.tv_collect, R.id.tv_invite, R.id.tv_answer, R.id.ll_consult})
    public void onClick(View view) {
        if (!isCanClick(view)) return;
        switch (view.getId()) {
            case R.id.tv_collect:
                //收藏
                mPresenter.collect();
                break;
            case R.id.tv_invite:
                advance(UserFriendsActivity.class, 3, question_id);
                break;
            case R.id.tv_answer:
                if (!Utils.checkIsLogin()) {
                    return;
                }
                mPresenter.goAnswer();
                break;
            case R.id.ll_consult:
                //畅谈通
                mPresenter.goConsult();
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * 窗口显示，窗口背景透明度渐变动画
     */
    @Override
    public void showAnimator() {
        transBg.setVisibility(View.VISIBLE);
        if (null != mShowAnimation) {
            mShowAnimation.cancel();
        }
        mShowAnimation = new AlphaAnimation(0.0f, 1.0f);
        mShowAnimation.setDuration(500);
        mShowAnimation.setFillAfter(true);
        mShowAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation arg0) {
                transBg.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

            }

            @Override
            public void onAnimationEnd(Animation arg0) {

            }
        });
        transBg.startAnimation(mShowAnimation);
    }

    private AlphaAnimation mShowAnimation;
    private AlphaAnimation mHideAnimation;

    /**
     * 窗口隐藏，窗口背景透明度渐变动画
     */
    @Override
    public void dismissAnimator() {
        if (null != mHideAnimation) {
            mHideAnimation.cancel();
        }
        // 监听动画结束的操作
        mHideAnimation = new AlphaAnimation(1.0f, 0.0f);
        mHideAnimation.setDuration(500);
        mHideAnimation.setFillAfter(true);
        mHideAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation arg0) {

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

            }

            @Override
            public void onAnimationEnd(Animation arg0) {
                transBg.setVisibility(View.GONE);
            }
        });
        transBg.startAnimation(mHideAnimation);
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.POST_DELETE_QUESTION)})
    public void afterDeleteQuesition(String data) {
        if (data.equals(String.valueOf(question_id))) {
            exit();
        }
    }

    @Override
    public boolean useRxBus() {
        return true;
    }

    @Override
    public PullToRefreshView getPullToRefreshView() {
        return commonRefresh;
    }

    @Override
    public RecyclerView getRecyclerView() {
        return commonRecycler;
    }

    @Override
    public void setNoData(boolean isNoData) {
    }

    @Override
    public int getQuestionId() {
        return question_id;
    }

    @Override
    public void setCollectionChanged(Boolean isCollect) {
        Drawable drawable;
        if (isCollect) {
            tvCollect.setText(R.string.collected);
            drawable = getResources().getDrawable(R.drawable.collect_bb_fill);
            tvCollect.setTextColor(ContextCompat.getColor(mActivity, R.color.messageRed));
        } else {
            tvCollect.setText(R.string.collect);
            drawable = getResources().getDrawable(R.drawable.collection_line);
            tvCollect.setTextColor(ContextCompat.getColor(mActivity, R.color.colorGray));
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvCollect.setCompoundDrawables(drawable, null, null, null);
    }

}
